<template>
  <div class="w">
    <div class="info">
      <div class="title" v-if="active != 3">找回密码</div>
      <div class="form">
        <div class="form_first">
          <el-form ref="firstRef" :model="loseForm" :rules="rules" v-if="active == 0">
            <el-form-item prop="phone">
              <el-input v-model="loseForm.phone" clearable placeholder="请输入手机号" class="input"></el-input>
            </el-form-item>
          </el-form>
          <div class="fotter" v-if="active == 0">
            <el-button @click="first" class="button" type="primary" :disabled="this.loseForm.phone == '' ? true : false">下一步</el-button>
          </div>
        </div>
        <div class="form_second">
          <el-form ref="secondRef" :model="loseForm" :rules="rules" v-if="active == 1">
            <el-form-item prop="phone">
              <el-input v-model="loseForm.phone" clearable="" placeholder="请输入完整手机号" class="input"></el-input>
            </el-form-item>
            <div class="form_input">
              <el-form-item prop="code">
                <el-input v-model="loseForm.code" clearable="" placeholder="请输入验证码" class="input_2"></el-input>
              </el-form-item>
              <div>
                <el-button class="text" @click="sendCode" :disabled="loseForm.phone == '' ? true : false">发送验证码</el-button>
              </div>
            </div>
          </el-form>
          <div class="fotter" v-if="active == 1">
            <el-button type="primary" class="button" @click="second" :disabled="loseForm.code == '' ? true : false">下一步</el-button>
          </div>
        </div>
        <div class="form_third" v-if="active == 2">
          <div class="form_title_2">设置新密码</div>
          <el-form ref="thirdtRef" :model="losePassword" :rules="rules">
            <el-form-item prop="password">
              <el-input v-model="losePassword.password" clearable="" placeholder="请输入新密码" class="input" type="password"></el-input>
            </el-form-item>
            <el-form-item prop="passwordTwo">
              <el-input v-model="losePassword.passwordTwo" clearable="" placeholder="请再次输入新密码" class="input" type="password"></el-input>
            </el-form-item>
          </el-form>
          <div class="notice">
            <span class="span_1">
              <i class="iconfont icon-chenggong"></i>
              密码由8位或8位以上数字、字母、特殊符号组成
            </span>
            <span class="span_2">
              <i class="iconfont icon-chenggong"></i>
              至少含有1中以上特殊字符
            </span>
          </div>
          <div class="fotter" v-if="active == 2">
            <el-button type="primary" class="button" @click="third" :disabled="losePassword.password == '' ? true : false">确定</el-button>
          </div>
        </div>
        <div class="success" v-if="active == 3">
          <div class="img">
            <i class="iconfont icon-chenggong1"></i>
          </div>
          <div class="tips">密码重置完成，页面将在3秒后自动跳转</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 手机号验证规则
    var checkPhone = (rule, value, cb) => {
      const regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (regPhone.test(value)) {
        return cb();
      }
      cb(new Error('手机号不合法'));
    };
    // 校验密码规则
    var checkPassword = (rule, value, cb) => {
      // 必须包含字母、数字、下划线的密码验证规则
      const regPassword = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$/;

      if (regPassword.test(value)) {
        return cb();
      }
      cb(new Error('密码必须包含数字、字母、特殊符号'));
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.losePassword.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      count: '',
      active: 0,
      loseForm: {
        phone: '',
        code: '',
      },
      losePassword: {
        id: '',
        password: '',
        passwordTwo: '',
        name: '',
      },
      rules: {
        // 手机验证规则
        phone: [
          { validator: checkPhone, trigger: 'blur' },
          { required: true, message: '手机号不能为空', trigger: 'blur' },
        ],
        //密码验证规则
        password: [
          { validator: checkPassword, trigger: 'blur' },
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        passwordTwo: [
          { validator: validatePass2, trigger: 'blur' },
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        // 验证码规则
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' },
        ],
      },
    };
  },

  created() {},

  methods: {
    first() {
      this.$refs.firstRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.get('user/getByPhone/' + this.loseForm.phone);
          if (res.code == 1) {
            this.active = this.active + 1;
            this.$refs.firstRef.resetFields();
            return;
          } else {
            return this.$message.error(res.msg);
          }
        }
      });
    },
    async sendCode() {
      const { data: res } = await this.$http.post('user/loginWithPhone', { phone: this.loseForm.phone });
      if (res.code == 1) {
        this.$message.success(res.data);
      } else {
        this.$message.error(res.msg);
      }
    },
    second() {
      this.$refs.secondRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('user/loginWithCode', { phone: this.loseForm.phone, code: this.loseForm.code });
          if (res.code == 1) {
            this.active = this.active + 1;
            this.$refs.secondRef.resetFields();
            this.losePassword.id = res.data.id;
            this.losePassword.userName = res.data.userName;
            return;
          } else {
            return this.$message.error(res.msg);
          }
        }
      });
    },
    third() {
      this.$refs.thirdtRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.put('user/updateUserPassword', this.losePassword);
          console.log(this.losePassword);
          if (res.code == 1) {
            this.active = this.active + 1;
            this.$refs.thirdtRef.resetFields();
            const timejump = 3;
            if (!this.timer) {
              this.count = timejump;
              this.show = false;
              this.timer = setInterval(() => {
                if (this.count > 0 && this.count <= timejump) {
                  this.count--;
                } else {
                  this.show = true;
                  clearInterval(this.timer);
                  this.timer = null;
                  //跳转的页面写在此处
                  this.$router.push('/login');
                }
              }, 1000);
            }
            return;
          } else {
            return this.$message.error(res.msg);
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.w {
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  background-color: #f5f5f5;
  .info {
    width: 305px;
    position: absolute;
    top: 100px;

    .title {
      font-size: 30px;
      font-weight: bold;
      margin-bottom: 40px;
      text-align: center;
    }
    .form {
      .input {
        :deep(.el-input__inner) {
          background-color: #fff;
          border: none;
          height: 50px;
          border-radius: 8.75px;
        }
      }
    }
  }
}
.button {
  margin-top: 12px;
  width: 300px;
  border-radius: 8.53px;
  background-color: #4094fe;
  color: #ffffff;
  background-image: -webkit-linear-gradient(325.15deg, #3382fd -20.29%, #64c4fe 107.31%);
  background-image: linear-gradient(124.85deg, #3382fd -20.29%, #64c4fe 107.31%);
}
.input_2 {
  :deep(.el-input__inner) {
    border-radius: 8.53px;
    background-color: #fff;
    width: 214px;
    height: 50px;
    border: none;
  }
}
.form_input {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .text {
    width: 100px;
    margin-bottom: 20px;
    background-color: transparent;
    border: none;
    color: #808080;
    font-size: 14px;
    margin-right: 5px;
  }
}
.form_title_2 {
  font-size: 18px;
  margin-bottom: 10px;
  float: left;
}
.notice {
  font-size: 12px;
  color: #b0b3bf;
  margin-bottom: 20px;
  position: relative;
  .span_1 {
    width: 300px;
    margin-bottom: 5px;
    float: left;
    line-height: 16px;
  }
  .span_2 {
    float: left;
  }
}
.success {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  flex-wrap: wrap;
  width: 300px;
  .icon-chenggong1 {
    width: 300px;
    color: #65b85d;
    font-size: 50px;
  }
  .tips {
    width: 300px;
    margin-top: 50px;
  }
}
</style>